package com.example.userclient.activity

import android.Manifest
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Matrix
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.baidu.mapapi.map.*
import com.baidu.mapapi.model.LatLng
import com.example.userclient.AddBook
import com.example.userclient.Main.ClientInfoHolder
import com.example.userclient.R
import com.example.userclient.databinding.ActivityBookDetailBinding
import permissions.dispatcher.NeedsPermission
import permissions.dispatcher.OnPermissionDenied
import permissions.dispatcher.RuntimePermissions


@RuntimePermissions
class BookDetailActivity : AppCompatActivity() {


    private var mapView:MapView? = null
    private var mmap:BaiduMap? = null
    private var book : AddBook?=null
    //获取权限
    @NeedsPermission(
        Manifest.permission.ACCESS_COARSE_LOCATION,
        Manifest.permission.ACCESS_FINE_LOCATION
    )
    fun getLocation() {//同意权限
    }
    @OnPermissionDenied(
        Manifest.permission.ACCESS_COARSE_LOCATION,
        Manifest.permission.ACCESS_FINE_LOCATION
    )
    fun refuseLocation(){

    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val bind = DataBindingUtil.setContentView<ActivityBookDetailBinding>(
            this,
                R.layout.activity_book_detail
        )
        val addBook = intent.extras?.get("addBook")

        mapView = bind.bmapView
        bind.addBook =addBook as AddBook

        book = addBook as AddBook
        bind.locDescribe = ClientInfoHolder.locationDescribe
        mmap = mapView!!.map

        bind.sendMessage.setOnClickListener{

            if (  book?.bookOwner == ClientInfoHolder.phone){
                  Toast.makeText(this,"无需向本人发送消息",Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            TalkActivity.actionStart(this, addBook)
        }

        init()
    }

    private fun  init (){


        mapView!!.map.mapType = BaiduMap.MAP_TYPE_NORMAL
        mapView!!.map.isMyLocationEnabled = true

        val update = MapStatusUpdateFactory.newLatLng(ClientInfoHolder.ll)
        mapView!!.map.animateMapStatus(update)


        val builder: MapStatus.Builder = MapStatus.Builder()
        builder.zoom(18.0f)
        mapView!!.map.setMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()))



        mapView!!.map.setMyLocationData(ClientInfoHolder.myloc)

        ClientInfoHolder.clientMapWithin1000?.let {
            val bitmap = BitmapDescriptorFactory
                .fromResource(R.drawable.local_client)


            for (i in it){
                val point = LatLng(i.position.latitude, i.position.longitude)

                val option: OverlayOptions = MarkerOptions()
                    .position(point)
                    .icon(bitmap)

                mapView!!.map.addOverlay(option);
            }
        }
        val targetBitMap = BitmapDescriptorFactory
            .fromResource(R.drawable.target_client)

        book?.position?.let {
            val point = LatLng(it.latitude, it.longitude)
            val option: OverlayOptions = MarkerOptions()
                .position(point)
                .icon(targetBitMap)

            mapView!!.map.addOverlay(option);
        }
    }
    fun zoomImg(bm: Bitmap, newWidth: Int, newHeight: Int): Bitmap? {
        // 获得图片的宽高
        val width = bm.width
        val height = bm.height
        // 计算缩放比例
        val scaleWidth = newWidth.toFloat() / width
        val scaleHeight = newHeight.toFloat() / height
        // 取得想要缩放的matrix参数
        val matrix = Matrix()
        matrix.postScale(scaleWidth, scaleHeight)
        // 得到新的图片
        return Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true)
    }
    override fun onResume() {
        super.onResume()
        init()
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mapView?.onResume()
    }

    override fun onPause() {
        super.onPause()
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mapView?.onPause()
    }

    override fun onDestroy() {


        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mapView?.onDestroy()
        mapView = null
        mmap?.isMyLocationEnabled= false
        super.onDestroy()
    }
    companion object{
        fun actionStart(context: Context, addBook: AddBook){
            val intent = Intent(context, BookDetailActivity::class.java)
            intent.putExtra("addBook", addBook)
            context.startActivity(intent)
        }
    }
}